package com.ruoyi.web.controller.system;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ConstructionEncloaure;
import com.ruoyi.system.service.IConstructionEncloaureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 施工附件Controller
 * 
 * @author ruoyi
 * @date 2021-06-21
 */
@RestController
@RequestMapping("/system/encloaure")
public class ConstructionEncloaureController extends BaseController
{
    @Autowired
    private IConstructionEncloaureService constructionEncloaureService;

    /**
     * 查询施工附件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:list')")
    @GetMapping("/list")
    public TableDataInfo list(ConstructionEncloaure constructionEncloaure)
    {
        startPage();
        List<ConstructionEncloaure> list = constructionEncloaureService.selectConstructionEncloaureList(constructionEncloaure);
        return getDataTable(list);
    }

    /**
     * 导出施工附件列表
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:export')")
    @Log(title = "施工附件", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ConstructionEncloaure constructionEncloaure)
    {
        List<ConstructionEncloaure> list = constructionEncloaureService.selectConstructionEncloaureList(constructionEncloaure);
        ExcelUtil<ConstructionEncloaure> util = new ExcelUtil<ConstructionEncloaure>(ConstructionEncloaure.class);
        return util.exportExcel(list, "encloaure");
    }

    /**
     * 获取施工附件详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:query')")
    @GetMapping(value = "/{ceId}")
    public AjaxResult getInfo(@PathVariable("ceId") Long ceId)
    {
        return AjaxResult.success(constructionEncloaureService.selectConstructionEncloaureById(ceId));
    }

    /**
     * 新增施工附件
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:add')")
    @Log(title = "施工附件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(ConstructionEncloaure constructionEncloaure, MultipartFile file)
    { try {
        String profile = RuoYiConfig.getProfile();
        byte[] bytes = file.getBytes();
        //文件夹
        Path path2 = Paths.get(profile+"/cId/" + constructionEncloaure.getcId()+"/"+constructionEncloaure.getCeType());
        //具体文件
        Path path = Paths.get(profile+"/cId/" + constructionEncloaure.getcId()+"/"+constructionEncloaure.getCeType() + "/" + file.getOriginalFilename());
        File file1 = path.toFile();
        //创建文件夹
        if (!file1.exists()) {
            path2.toFile().mkdirs();
        }
        if (file1.isFile()) {
            return AjaxResult.error("文件上传失败,文件存在");
        } else {
            Files.write(path, bytes);//创建文件
            constructionEncloaure.setCeUrl("profile/htId/" + constructionEncloaure.getcId()+"/"+constructionEncloaure.getCeType() + "/" + file.getOriginalFilename());
        }
    }catch (Exception e){

    }

        return toAjax(constructionEncloaureService.insertConstructionEncloaure(constructionEncloaure));
    }

    /**
     * 修改施工附件
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:edit')")
    @Log(title = "施工附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ConstructionEncloaure constructionEncloaure)
    {
        return toAjax(constructionEncloaureService.updateConstructionEncloaure(constructionEncloaure));
    }

    /**
     * 删除施工附件
     */
//    @PreAuthorize("@ss.hasPermi('system:encloaure:remove')")
    @Log(title = "施工附件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ceIds}")
    public AjaxResult remove(@PathVariable Long[] ceIds)
    {
        return toAjax(constructionEncloaureService.deleteConstructionEncloaureByIds(ceIds));
    }
}
